package com.jin.lazytool.utils;

import com.jin.lazytool.annotion.Call2ParFun;
import com.jin.lazytool.annotion.CallFun;
import com.jin.lazytool.entity.EventKeyConvertEntity;
import com.jin.lazytool.entity.IEvent;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;

import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class JinUtils {
    private static final Map<String, EventKeyConvertEntity> KEY_EVENT_CONVERT = new HashMap<>();
    private static final Globals GLOBALS = JsePlatform.standardGlobals();

    public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    static {
        Field[] fields = KeyEvent.class.getDeclaredFields();

        try {
            for (Field field : fields) {
                if(Modifier.isPublic(field.getModifiers())){
                    int anInt = field.getInt(null);
                    String keyText = KeyEvent.getKeyText(anInt);
                    if(!keyText.startsWith("未知")){
                        KEY_EVENT_CONVERT.put(keyText,new EventKeyConvertEntity(keyText, anInt));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public static Map<String, EventKeyConvertEntity> getEventKeyFields() {
        return KEY_EVENT_CONVERT;
    }

    public static void alert(String tip, long sleep) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示");
        alert.setContentText(tip);
        Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
        stage.getIcons().add(new Image("dialog.png"));
        Optional<ButtonType> result = alert.showAndWait();
        result.ifPresent(e -> {

        });

    }

    public static void alert(String tip) {
        alert(tip, 1000);
    }

    public static void error(String tip,String errMsg) {
        alert(tip, 1000);
        throw new RuntimeException(errMsg);
    }
    public static void error(String tip) {
        alert(tip, 1000);
        throw new RuntimeException(tip);
    }
    public static Globals getLuaEngine(){
        return  GLOBALS;
    }

    public static LuaTable evalLua(String script, long time){
        // Use the convenience function on Globals to load a chunk.
        getLuaEngine().load(script).call();
        // 调用Lua函数
        LuaValue luaFunction = getLuaEngine().get("jin");
        LuaValue call = luaFunction.call(LuaValue.valueOf(time));
        return (LuaTable) call;
    }

    public static GridPane getDialog(String title, Call2ParFun<GridPane,Dialog> initFun, CallFun okFun){
        Dialog<IEvent> dialog = new Dialog<>();
        Window window = dialog.getDialogPane().getScene().getWindow();
        Stage stage = (Stage) window;
        stage.getIcons().add(new Image("dialog.png"));
        dialog.setTitle(title);
        dialog.setResizable(true);
        ButtonType okButton = new ButtonType("确认", ButtonBar.ButtonData.OK_DONE);
        dialog.getDialogPane().getButtonTypes().addAll(okButton, ButtonType.CANCEL);
        ScrollPane scrollPane = new ScrollPane();
        GridPane grid = new GridPane();
        scrollPane.setContent(grid);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(20, 10, 10, 10));
        grid.getColumnConstraints().add(new ColumnConstraints(100));
        grid.getColumnConstraints().add(new ColumnConstraints(300));
        grid.setPrefWidth(500);
        grid.setPrefHeight(300);

        initFun.call(grid,dialog);
        dialog.setResultConverter(dialogButton -> {
            if (dialogButton == okButton) {
                if(okFun != null){
                    okFun.call();
                }
            }
            return null;
        });
        dialog.getDialogPane().setContent(scrollPane);
        Optional<IEvent> result = dialog.showAndWait();
        result.ifPresent(myActionEvent -> {

        });
        return grid;
    }
}
